# Copyright (C) 2009, 2011 Christofer Jonsson
#
# This makefile is part of FXH.
#
# This makefile is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This makefile is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this makefile.  If not, see
# <http://www.gnu.org/licenses/>.
#

#=====================================================================
# application
#=====================================================================

APP = fxh

#=====================================================================
# paths
#=====================================================================

BIN = bin
OBJ = obj
SRC = src

#=====================================================================
# variables
#=====================================================================

FLAVOR ?= release

#=====================================================================
# gcc
#=====================================================================

CC       = gcc
CFLAGS   = -Wall -Wextra -pedantic -Wno-long-long -Wno-variadic-macros -pthread
LFLAGS   = -pthread
DEFINES += -D_FILE_OFFSET_BITS=64

ifeq ($(FLAVOR),release)
  DEFINES += -DNDEBUG
endif

ifeq ($(FLAVOR),debug)
  CFLAGS  += -g
  DEFINES += -DDEBUG
endif

ifeq ($(FLAVOR),test)
  CFLAGS  += -g
  DEFINES += -DDEBUG -DTEST
endif

INCLUDES = $(addprefix -I,$(SRC))
SRCFILES = $(wildcard $(SRC)/*.c)
HDRFILES = $(wildcard $(SRC)/*.h)
OBJFILES = $(addprefix $(OBJ)/,$(patsubst %.c,%.o,$(notdir $(SRCFILES))))

#=====================================================================
# commands
#=====================================================================	

ECHO  = echo
MKDIR = mkdir
RM    = rm -fr

#=====================================================================
# targets
#=====================================================================

# phony
.PHONY: clean all

# silent
.SILENT:

# clean
clean:
	$(RM) $(OBJ) $(BIN)

# all
all: $(OBJ) $(BIN) $(BIN)/$(APP)
	$(ECHO) "Build done"

# obj
$(OBJ):
	$(ECHO) "Creating $@"
	$(MKDIR) $(OBJ)

# bin
$(BIN):
	$(ECHO) "Creating $@"
	$(MKDIR) $(BIN)

# link
$(BIN)/$(APP): $(OBJFILES)
	$(ECHO) "Linking $@"
	$(CC) $(LFLAGS) -o $@ $(OBJFILES)

# compile
$(OBJ)/%.o: $(SRC)/%.c $(HDRFILES)
	$(ECHO) "Compiling $<" 
	$(CC) $(DEFINES) $(CFLAGS) $(INCLUDES) -c $< -o $@
